package com.lycx.businessmanager.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Look implements ApplicationContextAware {

	private static final Logger logger = Logger.getLogger(Look.class);
	private static ApplicationContext applicationContext = null;
	private static Properties classProps = null;

	/** Creates a new instance of LOOK */
	public Look() {

	}
	
	/**
	 * 查找方法,根据class 查找环境中的bean,返回一个bean 对象
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static Object up(Class clazz) {
		if (logger.isDebugEnabled()) {
			logger.debug("系统使用了look.up(Class) 方法查找Class 为 " + clazz + " 的对象.");
		}

		Map map = applicationContext.getBeansOfType(clazz);
		Object[] arr = map.keySet().toArray();

		if (arr.length == 0) {
			logger.warn("Look.up(Class) 没有找到符合条件的对象.");
			return null;
		}

		// 配置中有其信息直接按配置的名字查找
		// 即使有名字也要其类型匹配
		if (classProps != null) {
			String name = classProps.getProperty(clazz.getName());
			if (name != null) {
				Object o = map.get(name);
				if (o == null) {
					logger.warn("Look.up(Class) 没有找到符合条件的对象.");
					return null;
				}
				return o;
			}
		}

		if (arr.length == 1)
			return map.get(arr[0]);
		else {
			logger.warn("Look.up(Class) 找到多个符合条件的对象,不能判断返回那一个.");
			return null;
		}
	}

	/**
	 * 查找方法,根据配置文件中的名字查找环境中的bean对象
	 */
	public static Object up(String objName) {
		if (logger.isDebugEnabled()) {
			logger.debug("系统使用了look.up(Name ) 方法查找名字为 " + objName + " 的对象.");
		}
		if (applicationContext == null) {
			applicationContext = new ClassPathXmlApplicationContext(
					"/applicationContext.xml");
		}
		return applicationContext.getBean(objName);
	}

	/**
	 * 查找方法,根据配置文件中的名字查找环境中的bean对象
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static List ups(Class clazz) {
		if (logger.isDebugEnabled()) {
			logger
					.debug("系统使用了look.ups(Class) 方法查找Class为 " + clazz
							+ " 的对象列表.");
		}
		Map map = applicationContext.getBeansOfType(clazz);

		Object[] keysArr = map.keySet().toArray();

		List objList = new ArrayList();

		for (int i = 0; i < keysArr.length; i++) {
			objList.add(map.get(keysArr[i]));
		}

		return objList;
	}

	/**
	 * 接口 ApplicationContextAware 的实现方法
	 */
	@SuppressWarnings("static-access")
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		logger.debug("************加载 ApplicationContext");
		this.applicationContext = applicationContext;
	}

	@SuppressWarnings("static-access")
	public ApplicationContext getApplicationContext() {
		return this.applicationContext;
	}

	public Properties getClassProps() {
		return classProps;
	}

	public void setClassProps(Properties aClassProps) {
		classProps = aClassProps;
	}

}